package technion.edu.picaboo;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class SimpleImage extends Component{
	private BufferedImage bi;
	private int width;
	private int height;
	private WritableRaster raster;
	JFrame f = null;
	String title = "SimpleImage - No Title";
	String[] metaData = null;
	
	public void load() {
		throw new Error("Not implemented yet");
	}
	
	public void create(int theWidth, int theHeight) {
		width = theWidth;
		height = theHeight;
		bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Raster ras = bi.getData();
		raster = ras.createCompatibleWritableRaster();
    	raster.setRect(ras);
    	bi.setData(raster);
	}	
	
	public void load(String filename) {
        try {        	
            bi = ImageIO.read(new File(filename));
            width = bi.getWidth(null);
            height = bi.getHeight(null);
            if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
                BufferedImage tempBi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                Graphics g = tempBi.getGraphics();
                g.drawImage(bi, 0, 0, null);
                bi = tempBi;
            }
        } catch (IOException e) {
            System.out.println("err");
        }
		Raster ras = bi.getData();
		raster = ras.createCompatibleWritableRaster();
    	raster.setRect(ras);
    	bi.setData(raster);
	}
	
	public int getRed(int x, int y){
		int[] iArray = null;
		return raster.getPixel(x, y, iArray)[0];
	}
	
	public int getGreen(int x, int y){
		int[] iArray = null;
		return raster.getPixel(x, y, iArray)[1];
	}
	
	public int getBlue(int x, int y){
		int[] iArray = null;
		return raster.getPixel(x, y, iArray)[2];
	}
	
	public void setRed(int x, int y, int value){
		if (value > 255) {
			value = 255;
		}
		bi.setRGB(x, y, bi.getRGB(x, y) & 0xFFFF | (value << 16));
	}
	public void setGreen(int x, int y, int value){
		if (value > 255) {
			value = 255;
		}
		bi.setRGB(x, y, bi.getRGB(x, y) & 0xFF00FF | (value << 8));
	}
	
	public void setBlue(int x, int y, int value){
		if (value > 255) {
			value = 255;
		}	
		bi.setRGB(x, y, bi.getRGB(x, y) & 0xFFFF00 | value);
	}
	
	public void setColor(int x, int y, int r, int g, int b){
		r = (r > 255) ? 255 : r;		
		g = (g > 255) ? 255 : g;
		b = (b > 255) ? 255 : b;
		int value = (r << 16) | (g << 8) | b;
		bi.setRGB(x, y, value);
	}
	
	public void setColor(int x, int y, Color color){
		int value = (color.getRed() << 16) | (color.getGreen() << 8) | color.getBlue();
		bi.setRGB(x, y, value);
	}
	
	public void show(){
        f = new JFrame(title);        
        f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });

        f.add(this);
        f.pack();
        f.setVisible(true);
	}
	
    public void paint(Graphics g) {
        g.drawImage(bi, 0, 0, null);
    }
    
    public void setTitle(String newTitle) {
    	title = "SimpleImage - " + newTitle;
    }
    
    public Dimension getPreferredSize() {
        if (bi == null) {
        	return new Dimension(100, 100);
        } else {
        	return new Dimension(bi.getWidth(null), bi.getHeight(null));
       }
    }
    
    public void refresh() {
    	bi.flush();
    	getGraphics().drawImage(bi, 0, 0, null);   
        f.repaint();
        f.pack();
    }
    
    public int width() {
    	return width;
    }
    
    public int height() {
    	return height;
    }

	public Color[][] getColors() {
		Color[][] result = new Color[width][height];
		for (int i = 0; i < width; ++i) {
			result[i] = new Color[height];
			for (int j = 0; j < height; ++j) {
				result[i][j] = getColor(i, j);				
			}		
		}		
		return result;
	}

	public Color getColor(int i, int j) {		
		return (new Color(getRed(i,j), getGreen(i,j), getBlue(i,j)));
	}

}
